
var vm = new Vue({
	el:'#rrapp',
	data:{
        $jstree : {},
        dataList:[],
		title: null,
		fsmsSend: {
            templateId:1,
            sendFlag:1,
            addressFlag:1,
            fileId:null,
            fileName:null,
            mobileNum:null
        },
        template: {},
        templateId: 1,
        templateList: [],
        active: 0,
        canvasNumList:[
            {'value': 1, 'text': '1个区块'},
            {'value': 2, 'text': '2个区块'},
            {'value': 3, 'text': '3个区块'},
            {'value': 4, 'text': '4个区块'},
            {'value': 5, 'text': '5个区块'},
            {'value': 6, 'text': '6个区块'}
        ],
        canvasNumMap:{},
        contentFlagList: [
            {'value': 1, 'text': '模板提供'},
            {'value': 2, 'text': '发送时提供'}
        ], 
        contentFlagMap:{},
        canvasTypeList: [
            {'value': 1, 'text': '视频'},
            {'value': 2, 'text': '图片'},
            {'value': 3, 'text': '文本'}
        ],
        canvasTypeMap:{},
        sendFlagList: [
        {'value': 1, 'text': '审批后发送'},
        {'value': 2, 'text': '定时发送'}
        ],
        sendFlagMap: {},
        addressFlagList: [
            {'value': 1, 'text': '录入文本'},
            {'value': 2, 'text': '通讯录选择'},
            {'value': 3, 'text': '上传号码'}
        ],
        addressFlagMap: {},
        sendFlagSearchList: {},
        checkStateList: [
        {'value': 1, 'text': '通过'},
        {'value': 2, 'text': '未通过'}
        ],
        checkStateMap: {},
        checkStateSearchList: {},
        sendStateList: [
        {'value': 0, 'text': '草稿'},
        {'value': 2, 'text': '提交审批'},
        {'value': 3, 'text': '已审批待发送'},
        {'value': 4, 'text': '已发送'}
        ],
        sendStateMap: {},
        sendStateSearchList: {},
        $jstree : {},
        imageUrl:null,
		rules: {
          templateId: [
            {required: true, message: '请输入模板ID', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
          sendFlag: [
            {required: true, message: '请选择发送标识', trigger: 'change'}
          ],
          test:[]
       }
	},
    mounted: function () {

        this.$jstree = $('#jstree');
        this.$jstree.jstree({'plugins':["checkbox"], 'core':{data:null}});


        this.canvasTypeMap = list2Map(this.canvasTypeList)
        this.addressFlagMap = list2Map(this.addressFlagList)
        this.contentFlagMap = list2Map(this.contentFlagList)
        this.sendFlagMap = list2Map(this.sendFlagList)
        this.checkStateMap = list2Map(this.checkStateList)
        this.sendStateMap = list2Map(this.sendStateList)
        this.getTemplateList();
        this.templateChange(1);

        this.jsonTree();

        $("#slimScroll").slimScroll({
            height: '350px'
        });
    },
	methods: {
        preOne: function(){

            this.active = this.active -1 ;

            if(this.active < 0)
            {
                this.active = 0;
            }
            if(this.active == 1)
            {
                //this.jsonTree();
            }
        },
        nextOne: function(){


            if(this.active == 0)
            {
                var list = this.template.cavnenList;
                for (var i=0;i<list.length;i++)
                {
                    if(list[i].contentFlag == 2){

                        var content = list[i].content;

                        if(list[i].content == null || list[i].content == "")
                        {
                            toastr.error("模板数据没有提供");
                            return;
                        }

                    }
                }

                this.active = this.active + 1 ;
                if(this.active > 2)
                {
                    this.active = 2;
                }

            }else
            if(this.active == 1)
            {
                if(this.fsmsSend.addressFlag == 1)
                {
                    if(this.fsmsSend.addressobile == null || this.fsmsSend.addressobile == "")
                    {
                        toastr.error("没有录入手机号码");
                        return;
                    }
                }

                if(this.fsmsSend.addressFlag == 2)
                {
                    var $jstree = $('#jstree').jstree(true);
                    var sel = $jstree.get_selected(false);//获得所有选中节点，返回值为数组

                    if(sel.length == 0)
                    {
                        toastr.error("没有选择通讯录");
                        return;
                    }
                    this.fsmsSend.addressIdList = sel;
                }

                if(this.fsmsSend.addressFlag == 3)
                {
                    if(this.fsmsSend.fileId == null || this.fsmsSend.fileId == "")
                    {
                        toastr.error("没有上传号码文件");
                        return;
                    }
                }

                this.fsmsSend.cavnenList = this.template.cavnenList

                var url = "../fsmssend/save";
                $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(vm.fsmsSend),
                    success: function (r) {
                        if (r.code === 0) {

                            vm.active = vm.active + 1 ;
                            if(vm.active > 2)
                            {
                                vm.active = 2;
                            }

                            toastr.success("已经保存到服务器");
                        } else {
                            toastr.error(r.message);
                            return;
                        }
                    }
                });

            }




        },
        openAll: function(){
            var $jstree = $('#jstree').jstree(true);
            $jstree.open_all();
        },
        closeAll: function(){
            var $jstree = $('#jstree').jstree(true);
            $jstree.close_all();
        },
        jsonTree: function () {
            //加载菜单树
            $.get("../fsmssend/addresslist" , function(r){
                if (r.code == 0) {
                    var jsonData = r.data
                    vm.$jstree.jstree(true).settings.core.data = jsonData;
                    vm.$jstree.jstree(true).refresh()

                }
            });
        },
        getTemplateList:function () {
            $.ajax({
                type: "POST",
                url: "../fsmssend/templist",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        vm.templateList = r.data;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
        templateChange:function (templateId) {
            $.ajax({
                type: "GET",
                url: "../fsmssend/templateinfo/"+templateId,
                success: function (r) {
                    if (r.code == 0) {
                        vm.template = r.data;
                        vm.fsmsSend.templateId = templateId
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
        handleExcelSuccess: function(res, file) {
            debugger;
            if (res.code == 0) {

                this.fsmsSend.fileId = res.data.fileId;
                this.fsmsSend.fileName = res.data.fileName;
                this.fsmsSend.mobileNum = res.data.mobileNum;

            } else {
                toastr.error(res.message);
            }
        },
        handleAvatarSuccess: function(res, file) {

            if (res.code == 0) {
                var id = res.data.id;
                var url = res.data.url;
                var fileId = res.data.fileId;

                var list = this.template.cavnenList;
                for (var i=0;i<list.length;i++)
                {
                    if(id == list[i].canvasId){
                        list[i].url = url;
                        list[i].content = fileId;
                    }
                }
            } else {
                toastr.error(res.message);
            }

        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../fsmssend/save" : "../fsmssend/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.fsmsSend),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['fsmsSendForm'].validate(vm.saveOrUpadateValid);
		},
		del: function (sendIds) {

			if(sendIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../fsmssend/delete",
				    data: JSON.stringify(sendIds),
				    success: function(r){
						if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		delOne: function (sendId) {

            var sendIds = [];
            sendIds[0] = sendId;

        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "GET",
        			url: "../fsmssend/delete/"+sendId,
        			success: function(r){
                        if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
        			}
        		});
        	});
        },
		getInfo: function(sendId){
			$.get("../fsmssend/info/"+sendId, function(r){
                vm.fsmsSend = r.data;
            });
		},
        handleCommand:function (commond) {
            if(commond=='d')
            {
                location.href = "../fsmssend/download?query=" + escape(JSON.stringify(vm.q));
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(commond=='a')
            {
                $("#data").tableExport({type:'csv'},'data');
                return;
            }

            if(commond=='b')
            {
                $("#data").tableExport({type:'txt'},'data');
                return;
            }

            if(commond=='c')
            {
                $("#data").tableExport({type:'excel',escape:false,
                    mso: {fileFormat:'xlsx',
                        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                        worksheetName: ['Data']}},'data');
                return;
            }
        },
        exportExl: function () {
            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            $("#data").tableExport({type:'excel',escape:false,
                mso: {fileFormat:'xlsx',
                    excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                    worksheetName: ['Data']}},'data');

        },
		reload: function (event) {
			this.isadd = false;
			this.showList = true;
            this.getDataList();
		}
	}
});
